/**
 * @author 董一华
 * @since 1.00
 * @version 1.00
 * 
 * 厂家组织下拉框控件
 * 使用说明：
 * jsp中使用范例(最短模式)：<htdz-tag:SelectVendorTag name="xxx" selected="${zzz}"/>
 * 参数说明：
 * 1. name可以不写,缺省值为vendorid
 * 2. id可以不写,缺省值为name的值
 * 3. range为厂家取值范围，可选项，缺省值为now。可以有以下取值:
 *    (1)all：所有厂家
 *    (2)valid：status='1'的厂家
 *    (3)now：productgroup中nowflag='1'的厂家
 * 4. onchange可以不写，缺省值为无
 * 5. firstoption为首个下拉框的text。可以不写，缺省值为"全部"；
 *    如果firstoption为MyConstant.SELECT_NO_FIRST_OPTION，则表示此下拉框不需要添加额外option
 * 6. firstvalue为首个下拉框的value。可以不写，缺省值为"MyConstant.SELECT_FIRST_VALUE"
 * 7. selected为下拉框选中的值，可以不写。
 * 8. disabled：是否禁用true/false，缺省值为false
 * 9. hidedesc：是否隐藏文字说明“厂家：”，缺省值为显示
 * 
 * 修订列表： v1.00 2011-01-14 
 * 			修订人：董一华
 * 			修订内容：初始版本
 * 
 */

package com.zjht.gmcc.bean.tag;

import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import com.zjht.gmcc.bean.util.MyConstant;
import com.zjht.gmcc.bean.util.MyFormat;
import com.zjht.gmcc.bean.util.MyJDBC;

public class SelectVendorTag extends TagSupport {
	private static final long serialVersionUID = 7343412093252888637L;
	private String name;
	private String id;
	private String range;
	private String firstoption;
	private String firstvalue;
	private String onchange;
	private String selected;
	private boolean disabled;
	private boolean hidedesc;// 是否隐藏文字说明

	public SelectVendorTag() {
	}

	@SuppressWarnings("static-access")
	public int doStartTag() throws JspException {
		String name = this.name;
		String id = this.id;
		String range = this.range;
		String firstoption = this.firstoption;
		String firstvalue = this.firstvalue;
		String onchange = this.onchange;
		String selected = this.selected;
		boolean disabled = this.disabled;
		boolean hidedesc = this.hidedesc;

		if (MyFormat.isStrNull(range)) {
			range = "now";
		}
		range = range.trim().toLowerCase();
		if (!"all".equals(range) && !"now".equals(range) && !"valid".equals(range)) {
			throw new JspException("range可以为空；如果不为空，则只能取值：all,now,valid");
		}

		if (MyFormat.isStrNull(name)) {
			name = "vendorid";
		}
		if (MyFormat.isStrNull(id)) {
			id = name;
		}
		StringBuilder html = new StringBuilder();
		if (!hidedesc) {
			html.append("厂家：");
		}
		html.append("<select name=\"");
		html.append(name);
		html.append("\"	id=\"");
		html.append(id);
		html.append("\" ");
		if (onchange != null && onchange.length() > 0) {
			html.append("onchange=\"");
			html.append(onchange);
			html.append("\"");
		}
		if (disabled)// 禁用
			html.append(" disabled ");
		html.append(">");
		if (!MyConstant.SELECT_NO_FIRST_OPTION.equals(firstoption))// 如果firstoption为MyConstant.SELECT_NO_FIRST_OPTION，则不需要添加额外的option
		{
			if (firstoption == null || firstoption.length() == 0)
				firstoption = "全部";

			if (firstvalue == null || firstvalue.length() == 0)
				firstvalue = MyConstant.SELECT_FIRST_VALUE;

			html.append("<option value=\"");
			html.append(firstvalue);
			html.append("\">");
			html.append(firstoption);
			html.append("</option>");
		}
		try {
			MyJDBC myJDBC = null;
			try {
				myJDBC = new MyJDBC();
				StringBuilder sql = new StringBuilder();
				PreparedStatement ps = null;
				ResultSet rs = null;
				sql.append("SELECT orgid,shortname FROM ORGANIZA o WHERE orgatypeid=?");
				if (!"all".equals(range))
					sql.append(" AND status='1'");
				if ("now".equals(range)) {
					sql.append("AND EXISTS (SELECT 1 FROM PRODUCTGROUP g,PRODUCTBRAND b WHERE b.vendorid=o.orgid AND g.brandcode=b.brandcode AND g.nowflag='1')");
				}
				sql.append(" ORDER BY shortname");
				ps = myJDBC.prepareStatement(sql.toString());
				ps.setInt(1, MyConstant.ORGTYPE_VENDOR);
				rs = ps.executeQuery();
				String vendorid = "";
				while (rs.next()) {
					vendorid = rs.getString(1);
					html.append("<option value=\"");
					html.append(vendorid);
					html.append("\" ");
					if (!MyFormat.isStrNull(selected) && vendorid.equals(selected))
						html.append("selected");
					html.append(">");
					html.append(rs.getString(2));
					html.append("</option>");
				}
				rs.close();
				rs = null;
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				MyJDBC.close(myJDBC);
			}
			html.append("</select>");
			pageContext.getOut().print(html.toString());
		} catch (Exception e) {
			throw new JspException(e.getMessage());
		}
		html=null;
		return this.SKIP_BODY;
	}

	public void setName(String newName) {
		name = newName;
	}

	public void setId(String newId) {
		id = newId;
	}

	public void setRange(String range) {
		this.range = range;
	}

	public void setFirstoption(String firstoption) {
		this.firstoption = firstoption;
	}

	public void setOnchange(String onchange) {
		this.onchange = onchange;
	}

	public void setSelected(String selected) {
		this.selected = selected;
	}

	public void setSelected(int selected) {
		this.selected = String.valueOf(selected);
	}

	public void setFirstvalue(String firstvalue) {
		this.firstvalue = firstvalue;
	}

	public void setDisabled(boolean disabled) {
		this.disabled = disabled;
	}

	public void setDisabled(String disabled) {
		this.disabled = (disabled != null && "true".equals(disabled.trim().toLowerCase()));
	}

	public void setHidedesc(boolean hidedesc) {
		this.hidedesc = hidedesc;
	}
}